package com.cloud.poetry.web.controller;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;

/**
 * @author mengxj
 */
@Controller
@RequestMapping("page")
public class PageController {

    @GetMapping("index")
    public ModelAndView index(){
        return this.success("index.html", null);
    }

    @GetMapping("reg")
    public ModelAndView reg(){
        return this.success("login/reg.html", null);
    }

    @GetMapping("login")
    public String login(){
        return "login/index.html";
    }

    @GetMapping("{path}/{page}")
    public ModelAndView service(@PathVariable("path") String path, @PathVariable("page") String page,
                                @RequestParam(value = "params", required = false) String params){
        return this.success(String.format("%s/%s.html", path, page), params);
    }

    @GetMapping("{path1}/{path2}/{page}")
    public ModelAndView service1(@PathVariable("path1") String path1, @PathVariable("path2") String path2,
                                 @PathVariable("page") String page,
                                 @RequestParam(value = "params", required = false) String params){
        return this.success(String.format("%s/%s/%s.html", path1, path2, page), params);
    }

    public <T> ModelAndView success(String viewName, T t){
        ModelAndView modelAndView = new ModelAndView();
        modelAndView.setViewName(viewName);
        modelAndView.addObject("res", t);
        return modelAndView;
    }
}
